﻿Public Class perImposto
    Inherits AcessoBd

    Public Function retornaValorICMSEstado(ByVal sEstado As String) As Decimal
        Dim sSql As String
        Dim datRetorno As New DataTable
        Dim dRetorno As Decimal = 0

        sSql = "SELECT ICMS" & Environment.NewLine &
               " FROM ICMS " & Environment.NewLine &
               "WHERE Estado = @Estado " & Environment.NewLine

        With MyBase.SQLCmd.Parameters
            .Clear()
            .AddWithValue("@Estado", sEstado)
        End With

        datRetorno = MyBase.executarConsulta(sSql)

        If Not datRetorno Is Nothing AndAlso datRetorno.Rows.Count > 0 Then
            dRetorno = Conversao.ToDecimal(datRetorno.Rows(0).Item("ICMS"))
        End If

        Return dRetorno

    End Function

    Public Function retornaICMSCliente(ByVal sCodCliente As String) As Decimal
        Dim sSql As String
        Dim datRetorno As New DataTable
        Dim dRetorno As Decimal = 0

        sSql = "SELECT ICMS" & Environment.NewLine &
               " FROM ICMS  " & Environment.NewLine &
               "    INNER JOIN Cliente ON ICMS.Estado = Cliente.Estado " & Environment.NewLine &
               "WHERE Cliente.CodCliente = @CodCliente " & Environment.NewLine

        With MyBase.SQLCmd.Parameters
            .Clear()
            .AddWithValue("@CodCliente", sCodCliente)
        End With

        datRetorno = MyBase.executarConsulta(sSql)

        If Not datRetorno Is Nothing AndAlso datRetorno.Rows.Count > 0 Then
            dRetorno = Conversao.ToDecimal(datRetorno.Rows(0).Item("ICMS"))
        End If

        Return dRetorno

    End Function

    Public Function selecionarFator(ByVal dICMS As Decimal, dIPI As Decimal) As DataTable
        Dim sSql As String
        Dim datRetorno As New DataTable

        sSql = " SELECT * " & Environment.NewLine &
               " FROM Fator " & Environment.NewLine &
               " WHERE 1=1 " & Environment.NewLine

        If dICMS > 0 Then
            sSql &= "   AND ICMS=@ICMS " & Environment.NewLine
        End If

        If dIPI > 0 Then
            sSql &= "   AND IPI=@IPI "
        End If

        With MyBase.SQLCmd.Parameters
            .Clear()
            If dICMS > 0 Then
                .AddWithValue("@ICMS", dICMS)
            End If

            If dIPI > 0 Then
                .AddWithValue("@ICMS", dIPI)
            End If
        End With

        datRetorno = MyBase.executarConsulta(sSql)

        If datRetorno.Rows.Count > 0 Then
            Return datRetorno
        Else
            Return Nothing
        End If

    End Function

End Class
